package com.anli1.service.impl;

import com.anli1.entity.dto.SessionwebUserDto;
import com.anli1.entity.vo.R;
import com.anli1.mapper.UserMapper;
import com.anli1.entity.pojo.User;
import com.anli1.service.IUserService;
import com.anli1.tool.TokenEncryption;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
    RedisTemplate redisTemplate;
@Autowired
    TokenEncryption tokenEncryption;

    @Override
    public R login(String account, String password) throws Exception {
        //查询redis缓存
        Object o = redisTemplate.opsForValue().get(account);
        if(o==null)
       {
            //弹出验证码-或者其他策略 查mysql
            //mysql也为空
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getAccount, account); // 使用 Lambda 表达式引用字段 lambdaQueryWrapper.ge(User::getAge, 18);
            User user = this.baseMapper.selectOne(lambdaQueryWrapper);
            o=user;
            if(user==null)
            {
                redisTemplate.opsForValue().set(account,"");
                return R.error("账号或密码错误");
                //缓存穿透--
            }
            //插入user缓存到redis
           redisTemplate.opsForValue().set(account,user);
        }
        //账号对了
    User user = (User) o;
        if(!user.getPassword().equals(password))
                {
                return R.error("账号或密码错误");
                }
                //登录成功给sessio
                SessionwebUserDto sessionwebUserDto = new SessionwebUserDto();
                sessionwebUserDto.setAccountId(user.getAccount());
               String token= tokenEncryption.getTokenEncryption(String.valueOf(sessionwebUserDto));
                return R.success(token);
                }

    @Override
    public R qiangjuan(User user) {
        return null;
    }

    @Override
    public R register(User user) {
        if(user==null||user.getAccount() == null||user.getPassword()==null)
        {
            return  R.error("请补全参数");
        }
        //各类校验
        Object o = redisTemplate.opsForValue().get(user.getAccount());
        if(o!=null)
        {
            return R.error("账号已存在");
        }
        ///后续做xxl-job
/*        String keyRegister = "register"+user.getAccount();
        redisTemplate.opsForValue().set(keyRegister,user.getPassword());*/
        int insert = this.baseMapper.insert(user);
        if(insert==0)
        {
            return  R.error("注册失败");
        }
        //即时缓存
        redisTemplate.opsForValue().set(user.getAccount(),user);
        return R.success("注册成功");
    }

    @Override
    public R publish(User user) {
        return null;
    }

}
